#include "SALT_Engine.h"

using namespace std;

#include "SDL/SDL.h"

namespace SALT{
	std::vector<Objects::DrawableObject*>	Engine::_drawObjects;
	std::vector<Objects::EventObject*>		Engine::_eventObjects;
	Graphics::SDLGraphicsManager*			Engine::_graphicsManager=NULL;
	bool						Engine::_running=true;
	void Engine::AddObject(SALT::Objects::Object* obj){
		Objects::Object::ObjectType t=obj->type();
		if(t==Objects::Object::ObjectType_Drawable || t==Objects::Object::ObjectType_DrawableAndEvent)
			Engine::_drawObjects.push_back((Objects::DrawableObject*)obj);
		if(t==Objects::Object::ObjectType_Event || t==Objects::Object::ObjectType_DrawableAndEvent)
			Engine::_eventObjects.push_back((Objects::EventObject*)obj);
	}
	void Engine::Init(SALT::Graphics::SDLGraphicsManager *graphicsManager){
		Engine::_graphicsManager=graphicsManager;
	}
	void Engine::Run(){
		SDL_Event ev;

		while(Engine::_running){
			while(SDL_PollEvent(&ev)){
				Engine::_graphicsManager->handleEvent((void*)&ev);
				switch(ev.type){
					case SDL_QUIT:
						Engine::_running=false;
						break;
				}
			}
			if(!Engine::_running)
				break;
			Engine::_graphicsManager->clear();
			for(unsigned int i=0,l=Engine::_drawObjects.size();i<l;i++){
				Objects::DrawableObject* dobj=Engine::_drawObjects[i];
				dobj->update();
				dobj->draw(Engine::_graphicsManager);
			}
			Engine::_graphicsManager->flip();
		}
	}
	void Engine::Quit(){
		Engine::_running=false;
		delete Engine::_graphicsManager;
		Engine::_graphicsManager=NULL;
	}
};
